package com.example.demo.checkReport.controller;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.checkReport.entity.CheckForm;
import com.example.demo.checkReport.entity.CheckHeader;
import com.example.demo.checkReport.service.impl.CheckFormServiceImpl;
import com.example.demo.checkReport.service.impl.CheckHeaderServiceImpl;
import com.example.demo.common.ResponseData;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lbyani
 * @since 2021-05-25
 */
@RestController
@RequestMapping("/checkReport/check-header")
@PreAuthorize("hasAnyAuthority('6')")
@CrossOrigin(origins = {"*"})
public class CheckHeaderController {

	@Autowired
	private CheckHeaderServiceImpl checkServiceImpl;
	
	@Autowired
	private CheckFormServiceImpl checkformServiceImpl;
	/**
	 * 查询盘点表的表头，降序排序
	 * @return
	 */
	@PostMapping("/getformheader")
	public ResponseData getAllCheckForm(@RequestParam("currentPage") int currentPage, 
			@RequestParam("pageSize") int pageSize) {
		
		return checkServiceImpl.getAllCheckHeader(currentPage, pageSize);
		
	}
	
	/**
	 * 新建盘点表
	 * @param companyName
	 * @param companyPrincipal
	 * @param checker
	 * @param checkDate
	 * @param keeper
	 * @return
	 */
	@GetMapping("/insformheader")
	public ResponseData insCheckForm(@RequestParam("companyName") String companyName, 
			@RequestParam("companyPrincipal") String companyPrincipal, @RequestParam("checker") 
	String checker, @RequestParam("checkDate") String checkDate, @RequestParam("keeper") 
	String keeper) {
		
		// 将数据赋予实体类
		CheckHeader check = new CheckHeader();
		check.setCompanyName(companyName);
		check.setCompanyPrincipal(companyPrincipal);
		check.setChecker(checker);
		check.setCheckDate(checkDate);
		check.setKeeper(keeper);
		
		// 插入数据
		try {
			checkServiceImpl.save(check);
		} catch (Exception e) {
			// TODO: handle exception
		
			return ResponseData.buildError("新建失败！");
		}
		
		return ResponseData.buildOk("新建成功！");
	}
	
	/**
	 * 根据id 删除表头和相应 盘点表内信息
	 * @param id
	 * @return
	 */
	@GetMapping("/delHeaderById")
	public ResponseData delCheckHeaderById(@RequestParam("id") int id) {
		
		try {
			
			QueryWrapper<CheckForm> wrapper = new QueryWrapper<CheckForm>();
			wrapper.eq("head_id", id);
			// 在盘点表中删除表头id为 id 的数据
			checkformServiceImpl.remove(wrapper);
			
			// 删除表头
			checkServiceImpl.removeById(id);
			
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
			return ResponseData.buildError("删除失败！");
		}
		
		return ResponseData.buildOk("删除成功！");
	}
	
	/**
	 * 导出盘点表文档
	 * @param request
	 * @param response
	 */
	@GetMapping("/derivexcel")
	  public void getAll(HttpServletRequest request,HttpServletResponse response){
		
		try {
			checkServiceImpl.getAll(request,response);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  }
}

